package main

import (
	"fmt"
)

func fib(n int) int {
	var res int
	for i := 1; i <= n; i++ {
		res += i
	}
	return res
}

func put(intChan chan int) {
	for i := 1; i < 2000; i++ {
		intChan <- i
	}
	close(intChan)
}

func get(intChan chan int, res chan int, exit chan bool) {
	for {
		n, ok := <-intChan
		if ok {
			res <- fib(n)
		} else {
			break
		}
	}
	fmt.Println("一个协程结束")
	exit <- true
}

func doSumTest() {
	var intChan chan int = make(chan int, 100)
	var res chan int = make(chan int, 100)
	var exit chan bool = make(chan bool, 8)
	go put(intChan)
	for i := 0; i < 8; i++ {
		go get(intChan, res, exit)
	}
	go func() {
		for i := 0; i < 8; i++ {
			<-exit
		}
		close(res)
	}()
	//遍历我们的 primeChan ,把结果取出
	for {
		sum, ok := <-res
		if !ok {
			break
		}
		//将结果输出
		fmt.Println(sum)
	}
	fmt.Println("main done")
}
